//
//  MyWalletPaymentDetailsTableVC.m
//  InviteJob
//
//  Created by LangLang on 17/10/25.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "MyWalletPaymentDetailsTableVC.h"
#import "MyWalletPaymentDetailsView.h"

@interface MyWalletPaymentDetailsTableVC ()

{

    MyWalletPaymentDetailsView *heardView;
    NSString *_payWay;
}

@end

@implementation MyWalletPaymentDetailsTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TitleView(@"我要充值")
    
    _payWay = @"wxpay";

    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = strColor(@"f6f6f6");
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WXPayNotification:) name:@"WXPayNotification" object:nil];
  
}
-(void)dealloc{
        [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setCell"];
    if (!cell) {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"setCell"];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HEIGHT;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    heardView =[[MyWalletPaymentDetailsView alloc]initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT)];
    // 充值金额
    heardView.textFieldPrice.text = @"";
    // 微信
    [heardView.btnWeixinPay addTarget:self action:@selector(setBtnWeixinPays:) forControlEvents:UIControlEventTouchUpInside];
    heardView.btnWeixinPay.selected = YES;
    heardView.btnWeixinPay.tag = 1000;
    // 支付宝
    [heardView.btnAlipay addTarget:self action:@selector(setBtnAlipays:) forControlEvents:UIControlEventTouchUpInside];
    heardView.btnAlipay.tag = 1001;
    //支付
    [heardView.btnPay addTarget:self action:@selector(OnClickPay) forControlEvents:UIControlEventTouchUpInside];
    
    return heardView;
}
// 微信
-(void)setBtnWeixinPays:(UIButton *)sender{
    UIButton *btnAlipay = [self.view viewWithTag:1001];
    btnAlipay.selected = NO;
    sender.selected = YES;
    _payWay = @"wxpay";
}
 // 支付宝
-(void)setBtnAlipays:(UIButton *)sender{
    UIButton *btnWeixin = [self.view viewWithTag:1000];
    btnWeixin.selected =NO;
    sender.selected=YES;
    _payWay = @"alipay";
}

//支付
-(void)OnClickPay{
    
    if ([heardView.textFieldPrice.text isEqualToString:@""]) {
        SHuiMBhudTextTimeStr(@"充值金额不能为空");
        return;
    }
    
    [self payRequest];
    
    
}



// 支付请求
-(void)payRequest{
    
    isUser
    
    NSDictionary *dic = @{@"user_id":user.user_id,
                          @"token":user.token,
                          @"price":heardView.textFieldPrice.text,
                          @"pay_type":_payWay};
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(UnifiedRecharge_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            if ([_payWay isEqualToString:@"alipay"]) {
                NSString *appScheme = @"invitejob";
                
                [[AlipaySDK defaultService]payUrlOrder:responseObject[@"datas"][@"pay_string"] fromScheme:appScheme callback:^(NSDictionary* result) {
                    
                    if ([result[@"resultCode"] isEqualToString:@"9000"]) {
                        [SHJudgeTools showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:@"支付结果" message:@"支付成功" CallBackBlock:^(NSInteger btnIndex) {
                            
                        } cancelButtonTitle:nil destructiveButtonTitle:@"确定" otherButtonTitles:nil];
                        
                    }else if ([result[@"resultCode"] isEqualToString:@"6001"]) {
                        [SHJudgeTools showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:@"支付结果" message:@"用户取消" CallBackBlock:^(NSInteger btnIndex) {
                            
                        } cancelButtonTitle:nil destructiveButtonTitle:@"确定" otherButtonTitles:nil];
                    }else{
                        [SHJudgeTools showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:@"支付结果" message:@"支付异常" CallBackBlock:^(NSInteger btnIndex) {
                            
                        } cancelButtonTitle:nil destructiveButtonTitle:@"确定" otherButtonTitles:nil];
                    }
                }];
            }else{
                
                PayReq* req             = [[PayReq alloc] init];
                NSMutableDictionary *dict=[responseObject[@"datas"] objectForKey:@"Appparam"];
                req.openID              = [dict objectForKey:@"appid"];
                req.partnerId           = [dict objectForKey:@"partnerid"];
                req.prepayId            = [dict objectForKey:@"prepayid"];
                req.nonceStr            = [dict objectForKey:@"noncestr"];
                req.timeStamp           = [[dict objectForKey:@"timestamp"] intValue];
                req.package             = [dict objectForKey:@"packages"];
                req.sign                = [dict objectForKey:@"sign"];
                [WXApi sendReq:req];
                
            }
            
           
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
}

-(void)WXPayNotification:(NSNotification *)sender{
    
    NSString *WXCode = sender.userInfo[@"WXPayNotification"];
    
    if ([WXCode intValue] == 1) {
        [SHJudgeTools showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:@"支付结果" message:@"支付成功" CallBackBlock:^(NSInteger btnIndex) {
            
        } cancelButtonTitle:nil destructiveButtonTitle:@"确定" otherButtonTitles:nil];
    }
    
    if ([WXCode intValue] == 2) {
        [SHJudgeTools showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:@"支付结果" message:@"用户取消" CallBackBlock:^(NSInteger btnIndex) {
            
        } cancelButtonTitle:nil destructiveButtonTitle:@"确定" otherButtonTitles:nil];
    }
    
    if ([WXCode intValue] == 3) {
        [SHJudgeTools showAlertCntrollerWithViewController:self alertControllerStyle:UIAlertControllerStyleAlert title:@"支付结果" message:@"支付异常" CallBackBlock:^(NSInteger btnIndex) {
            
        } cancelButtonTitle:nil destructiveButtonTitle:@"确定" otherButtonTitles:nil];
    }
    
    
    
    
}

@end
